package com.zzvcom.wc;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 处理mapper处理过的数据
 */
public class WordCountReduce extends Reducer<Text, LongWritable, Text, LongWritable> {
    private static final Logger log = LoggerFactory.getLogger(WordCountReduce.class);
    /**
     * 重新reduce方法,把maper结果累计后进行输出。
     * @param key     就是每一个单词
     * @param values  mapper中搜集的集合
     * @param context 输出的reduce结果
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        long sum = 0;
        for (LongWritable value : values) {
            long i = value.get();
            sum += i;
        }
        context.write(key, new LongWritable(sum));
    }
}
